<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item
          label="流程名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="flowName"
        >
          <a-input v-model="model.flowName" placeholder="请输入流程名称"></a-input>
        </a-form-model-item>
        <a-form-model-item
          label="流程动作(收包/发包)"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="action"
        >
          <j-dict-select-tag
            type="list"
            v-model="model.action"
            dictCode="action"
            @change="changeAction"
            placeholder="请选择流程动作(收包/发包)"
          />
        </a-form-model-item>
        <a-form-model-item
          label="是否自动发包"
          v-if="model.action == 0 || model.action == 2"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="autoSend"
        >
          <j-dict-select-tag
            type="list"
            v-model="model.autoSend"
            dictCode="auto_send"
            @change="changeAction"
            placeholder="请选择是否自动发包"
          />
        </a-form-model-item>
        <!--          v-if="model.action == 0 || model.action == 2"-->
        <a-form-model-item
          label="连接"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="channelId"
        >
          <a-select
            placeholder="请选择"
            show-search
            optionFilterProp="children"
            v-model="model.channelId"
          >
            <a-select-option
              v-for="mode in channelList"
              :key="mode.id"
              :value="mode.id"
            >{{ mode.channelName }}</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item
          label="包id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="packageInfoId"
        >
          <!-- <a-input v-model="model.packageInfoId" placeholder="请选择包"></a-input> -->

          <a-select
            placeholder="请选择"
            show-search
            optionFilterProp="children"
            v-model="model.packageInfoId"
          >
            <a-select-option
              v-for="mode in packageInfoList"
              :key="mode.id"
              :disabled="mode.checked"
              :value="mode.id"
            >{{ mode.packageName }}</a-select-option>
          </a-select>
          <!-- <j-dict-select-tag type="list" v-model="model.packageInfoId" dictCode="" placeholder="请选择包id" /> -->
        </a-form-model-item>
        <a-form-model-item
          label="回调处理类名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="handle"
        >
          <a-input v-model="model.handle" placeholder="请输入回调处理类名称"></a-input>
        </a-form-model-item>
        <a-form-model-item
          label="流程定义key"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="processDefineKey"
        >
          <a-input v-model="model.processDefineKey" placeholder="请输入流程定义key"></a-input>
        </a-form-model-item>
        <a-form-model-item label="父级节点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pid">
          <j-tree-select
            ref="treeSelect"
            placeholder="请选择父级节点"
            v-model="model.pid"
            dict="nmc_flow_define,flow_name,id"
            pidField="pid"
            pidValue="0"
            hasChildField="has_child"
          ></j-tree-select>
        </a-form-model-item>
        <a-form-model-item label="是否多实例" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="multi" v-if='model.enableFlow === "Y"'>
          <j-dict-select-tag type="list" v-model="model.multi" dictCode="multi" placeholder="请选择" />
        </a-form-model-item>
        <a-form-model-item
          label="是否启用流程"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="enableFlow"
        >
          <j-dict-select-tag
            type="list"
            v-model="model.enableFlow"
            dictCode="enable_flow"
            placeholder="请选择"
          />
        </a-form-model-item>
        <a-form-model-item
          label="时序"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          prop="sequential"
          v-if='model.enableFlow === "Y"'
        >
          <a-input-number
            style="width:100%"
            :min="0"
            v-model="model.sequential"
            placeholder="请输入时序,数值越大,优先级越高"
          ></a-input-number>
          <!-- <j-dict-select-tag type="list" v-model="model.multi" dictCode="multi" placeholder="请选择" /> -->
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
// import { validateDuplicateValue } from '@/utils/util'
export default {
  name: 'NmcFlowDefineModal',
  components: {},
  data() {
    return {
      title: '操作',
      width: 800,
      visible: false,
      model: { packageInfoId: null, enableFlow: 'Y' },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      packageInfoList: [],
      channelList: [],
      confirmLoading: false,
      validatorRules: {
      },
      url: {
        add: '/flow/nmcFlowDefine/add',
        edit: '/flow/nmcFlowDefine/edit',
        Packagelist: '/grid/nmcPackageInfo/queryPackagesOfUnConfiguredInFlow',
        channelList: '/channel/nmcChannelInfo/list',
      },
      expandedRowKeys: [],
      pidField: 'pid',
      modelDefault: { packageInfoId: null },
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    changeAction() {
      this.model.packageInfoId = ''
      this.getpackageList()
    },
    getpackageList() {
      //获取未配置入流程的工程量包全部列表
      httpAction(this.url.Packagelist, { pageSize: -1, processDefineId: this.model.id, flowAction: this.model.action, autoSend: this.model.autoSend }, 'post').then((res) => {
        //该包的工程量列表
        this.packageInfoList = res.result
      })
      //获取连接列表
      getAction(this.url.channelList, { pageSize: -1 }).then((res) => {
        console.log(res, 'res')
        this.channelList = []
        this.channelList = res.result.records
      })
    },
    add(obj) {
      this.modelDefault.pid = ''
      this.edit(Object.assign(this.modelDefault, obj))
    },
    edit(record) {

      this.model = Object.assign({}, record)
      this.visible = true
      this.getpackageList()
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          if (this.model.id && this.model.id === this.model[this.pidField]) {
            that.$message.warning('父级节点不能选择自己')
            that.confirmLoading = false
            return
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                this.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    submitSuccess(formData, flag) {
      if (!formData.id) {
        let treeData = this.$refs.treeSelect.getCurrTreeData()
        this.expandedRowKeys = []
        this.getExpandKeysByPid(formData[this.pidField], treeData, treeData)
        this.$emit('ok', formData, this.expandedRowKeys.reverse())
      } else {
        this.$emit('ok', formData, flag)
      }
    },
    getExpandKeysByPid(pid, arr, all) {
      if (pid && arr && arr.length > 0) {
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].key == pid) {
            this.expandedRowKeys.push(arr[i].key)
            this.getExpandKeysByPid(arr[i]['parentId'], all, all)
          } else {
            this.getExpandKeysByPid(pid, arr[i].children, all)
          }
        }
      }
    },
  },
}
</script>
